package com.chaoyong.higo.cainiaowo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public abstract class BaseRecyclerAdapter<T, H extends BaseRViewHolder> extends
		RecyclerView.Adapter<BaseRViewHolder> {

	protected static final String TAG = BaseRecyclerAdapter.class
			.getSimpleName();

	protected final Context context;

	protected int layoutResId;

	protected List<T> datas;

	private OnItemClickListener mOnItemClickListener;

	public interface OnItemClickListener {
		void onItemClick(View view, int position);
	}

	public BaseRecyclerAdapter(Context context, int layoutResId) {
		this(context, layoutResId, null);
	}

	public BaseRecyclerAdapter(Context context, int layoutResId, List<T> datas) {
		this.datas = datas == null ? new ArrayList<T>() : datas;
		this.context = context;
		this.layoutResId = layoutResId;
	}

	@Override
	public BaseRViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
		View view = LayoutInflater.from(context).inflate(layoutResId,
				viewGroup, false);
		BaseRViewHolder vh = new BaseRViewHolder(view, mOnItemClickListener);
		return vh;
	}

	@Override
	public void onBindViewHolder(BaseRViewHolder viewHolder, int position) {
		T item = getItem(position);
		convert((H) viewHolder, item);
	}

	@Override
	public int getItemCount() {
		if (datas == null || datas.size() <= 0)
			return 0;

		return datas.size();
	}

	public T getItem(int position) {
		if (position >= datas.size())
			return null;
		return datas.get(position);
	}

	public void clear() {
		for (Iterator it = datas.iterator(); it.hasNext();) {
			T t = (T) it.next();
			int position = datas.indexOf(t);
			it.remove();
			notifyItemRemoved(position);
		}
	}

	public void removeItem(T t) {
		int position = datas.indexOf(t);
		datas.remove(position);
		notifyItemRemoved(position);
	}

	public List<T> getDatas() {
		return datas;
	}

	public void addData(List<T> datas) {
		addData(0, datas);
	}

	public void addData(int position, List<T> list) {
		if (list != null && list.size() > 0) {
			for (T t : list) {
				datas.add(position, t);
				notifyItemInserted(position);
			}
		}
	}

	public void refData(List<T> list) {
		if (list != null && list.size() > 0) {
			clear();
			int size = list.size();
			for (int i = 0; i < size; i++) {
				datas.add(i, list.get(i));
				notifyItemInserted(i);
			}
		}
	}

	public void loadMoreData(List<T> list) {
		if (list != null && list.size() > 0) {
			int size = list.size();
			int begin = datas.size();
			for (int i = 0; i < size; i++) {
				datas.add(list.get(i));
				notifyItemInserted(i);
			}
		}
	}

	protected abstract void convert(H viewHolder, T item);

	public void setOnItemClickListener(OnItemClickListener listener) {
		this.mOnItemClickListener = listener;
	}
}
